//
////#include "GridSingleton.hpp"
//
////#include "base/base64.hpp"
//
//#include <iostream>
//#include <vector>
//#include "GRIDAlgorithm.hpp"
//
//int main()
//{
//
//	//CloudPointData data(
//	//	120.123456,
//	//	30.12345,
//	//	20,
//	//	30,
//	//	40,
//	//	99,
//	//	"zunceng");
//
//	//GRIDAlgorithm::GRID grid11(11, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid12(12, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid13(13, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid14(14, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid15(15, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid16(16, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid17(17, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid18(18, 120.123456, 30.123456);
//	//GRIDAlgorithm::GRID grid19(19, 120.123456, 30.123456);
//	//GridSingleton::getInstance()->getCloudsMap().insert(data.getJID(), data);
//	//for (int i =0;;i++)
//	//{
//	//	
//
//	//	std::list<CloudPointData> data_list;
//	//	GridSingleton::getInstance()->getCloudsMap().getDataByGrid(grid, data_list);
//	//	if (i % 10000 == 0)
//	//	{
//	//		std::cout << i << std::endl;
//	//	}
//	//}
//	
//	//CCTool::CBase64 base64test;
//	////
//	////std::string str = "[187055691, 187046406, 187055688, 187046404, 187055689, 187046403, 187055655, 187048554, 187055619, 187046387, 187055620, 187046370, 187055664, 187046391, 187055822, 187803177, 187058783, 187047861, 187058782, 187047832, 187057995, 187047850, 187057994, 187047474, 187081933, 187049532, 187057955, 187047465, 187057903, 187047455, 187057902, 187047430, 187057904, 187047431, 187057943, 187047449, 187058366, 187803139, 187058766, 187047816, 187058378, 187047815, 187058050, 187803140, 187058051, 187803134, 187056090, 187803141, 187056089, 187803030, 187058172, 187047571, 187058174, 187047542, 187059294, 187049131, 187082013, 187049606, 187058309, 187048185, 187058061, 187803378]";
//
//
//	//int arr[] = {187055691, 187046406, 187055688};
//
//	//int N = sizeof(arr) / sizeof(int);
//
//	//int len = sizeof(arr);
//
//	////std::string str2 = "Sz4mCwYaJgtIPiYL";
//
//	//std::cout << base64test.Encode((unsigned char *)arr, len) << std::endl;
//
//	//int a;
//	//std::cout << base64test.Decode(str2.c_str(), str.length(),a) << std::endl;
//
//
//	//std::string strt = "k0UmCzgmJguURSYLbh0mC8JLJguBICYLGqUmC4QgJgspRSYLTyYmCy2JMQtvHSYLjUUmC7kMMAuZRSYLyqUxCw1GJgujHSYLGEUmC8KlMQsrRiYL3KUxCy5GJgvepTELSEYmC7gdJgtmRiYLkKYxCwNFJgs4IyYLAkUmCzEdJgtbRSYLw6UxC1xFJgtXHSYLxEYmCzymMQvIRiYL8B0mC\/BGJgsHHiYLF0cmCxkeJgu+SCYLA6YxC05KJguIHyYLykgmC4cfJguCRyYLBKYxC4NHJgv+pTEL2j8mCwWmMQvXPyYLlqUxC9s\/JguXpTEL5T8mC5ClMQt\/TSYL1yEmC1k\/JgtfpjELWD8mC0+mMQs=";
//
//	//CCTool::CBase64 base64;
//
//	//int outlen = 0;
//	//std::string de_str = base64.Decode(strt.c_str(), strt.length(), outlen);
//
//	//int n = outlen / sizeof(int);
//
//	//int *arr_i = new int[n];
//
//	//memcpy(arr_i, de_str.c_str(), outlen);
//
//	//std::vector<unsigned int> t_vec(arr_i,arr_i + n);
//
//	
//
//
//	
//}
//
//


#include <base/gridfunc.hpp>

namespace CCTool{

	namespace detail{
		enum{ SEARCH_ZOOM = 14};

		static unsigned short getX(int grid_id)
		{
			return (unsigned short)((grid_id & 0xffff0000) >> 16);
		}

		static unsigned short getY(int grid_id)
		{
			return (unsigned short)(grid_id & 0x0000ffff);
		}

	}// end of CCTool::detail

	unsigned int geneGridIDfromXY(unsigned short x, unsigned short y)
	{
		return (x << 16) | y;
	}
	unsigned int geneGridIDfromGPS(double lng, double lat)
	{
		int x = GridFunc::getTileNumberX(detail::SEARCH_ZOOM,lng);
		int y = GridFunc::getTileNumberY(detail::SEARCH_ZOOM,lat);

		return geneGridIDfromXY(x, y);
	}



	void getGridIDMatrix(unsigned int gridid, unsigned int *output, int N)
	{
		unsigned short x = detail::getX(gridid);
		unsigned short y = detail::getY(gridid);

		//	int n = _msize(output);

		//	assert( n == N * N);

		assert( N > 0 && N < 19 && N % 2);

		for (int i = 0 ;i < N; i++)
			for (int j = 0; j < N; j++)
			{
				output[i* N + j] = geneGridIDfromXY(x - N/2 + i, y - N/2 + j);
			}
	}

	unsigned int getOutputArraySizeinMatrixEdge(int N)
	{
		assert(N > 0);

		if (N == 1)
		{
			return 1;
		}
		return (N-1) * 4;
	}

	void getGridIDMatrixEdge(unsigned int gridid, unsigned int *output, int N)
	{
		unsigned short x = detail::getX(gridid);
		unsigned short y = detail::getY(gridid);

		int len = 2 * N - 1;
		int Nd2 = N - 1;
		int n = 0;

		if(N == 1)
		{
			output[0] = gridid;
		}
		else
		{
			//edge
			for(int i = 1 ; i < len - 1 ; i++)
			{
				// down
				output[n++] = geneGridIDfromXY(x + Nd2 - i, y + Nd2 );
				// right
				output[n++] = geneGridIDfromXY(x + Nd2, y - Nd2 + i);
				// left
				output[n++] = geneGridIDfromXY(x - Nd2, y + Nd2 - i);
				// up
				output[n++] = geneGridIDfromXY(x - Nd2 + i, y - Nd2);
			}
			//Angle
			output[n++] = geneGridIDfromXY(x + Nd2, y + Nd2);
			output[n++] = geneGridIDfromXY(x + Nd2, y - Nd2);
			output[n++] = geneGridIDfromXY(x - Nd2, y + Nd2);
			output[n++] = geneGridIDfromXY(x - Nd2, y - Nd2);
		}	
	}

}// end of CCTool

int main()
{
	unsigned int gridid = CCTool::geneGridIDfromGPS(120.123456, 30.123456);

	int n = 11;

	unsigned int *arr = new unsigned int[n * n];

	CCTool::getGridIDMatrix(gridid, arr, 11);


	int N = CCTool::getOutputArraySizeinMatrixEdge(5);
	unsigned int *v_arr = new unsigned int[N];
	CCTool::getGridIDMatrixEdge(gridid, v_arr, 5);

	for (int i = 0 ; i < N; i++)
	{
		printf("%X\n", v_arr[i]);
	}

	delete[] arr;
	delete[] v_arr;

}